/*
 * Copyright (C) 20015 MaiNaEr All rights reserved
 */
package com.mainaer.wjokhttp.controller;


import com.mainaer.wjokhttp.model.MovieListResponse;
import com.mainaer.wjokhttp.comment.MyAppController;
import com.mainaer.wjokhttp.model.MovieListRequest;
import com.mainaer.wjokhttp.comment.URLConst;
import com.mainaer.wjoklib.okhttp.IUrl;
import com.mainaer.wjoklib.okhttp.OKBaseResponse;
import com.mainaer.wjoklib.okhttp.exception.OkHttpError;


/**
 * movie列表请求控制器
 *
 * @author wangjian
 * @date 2016/3/23.
 */
public class MovieListController extends MyAppController<MovieListController.MovieListListener> {

    public MovieListController(MovieListListener l) {
        mListener = l;
    }

    public void loadMovieList(String query, int page) {
        MovieListRequest request = new MovieListRequest();
        request.page = page;
        request.s = query;
        Task task = new Task();
        task.load(request, MovieListResponse.class);
    }

    @Override
    public Class<? extends OKBaseResponse> getBaseResponseClass() {
        // 此处由于movie数据格式特殊，需要自定义base类
        return MovieListResponse.class;
    }

    /**
     * 执行加载任务的task，其回调都是在主线程中调用
     * 需要加入请求参数LoadRequest和相应参数LoadResponse
     */
    private class Task extends AppBaseTask<MovieListRequest, MovieListResponse> {

        @Override
        public IUrl getUrl() {
            // 获取url
            return URLConst.Movie.MOVIE_LIST;
        }

        @Override
        public void onSuccess(MovieListResponse loadResponse) {
            // 获得加载成功的相应数据，自动解析成LoadResponse
            mListener.onLoadMovieListSuccess(loadResponse);
        }

        @Override
        public void onError(OkHttpError e) {
            // 加载失败回调
            mListener.onLoadMovieListFail(e);
        }
    }

    /**
     * 加载回调接口
     */
    public interface MovieListListener {

        void onLoadMovieListSuccess(MovieListResponse loadResponse);

        void onLoadMovieListFail(OkHttpError e);
    }
}
